package com.LearnInWorld.page.controller;

import com.LearnInWorld.entity.Result;
import com.LearnInWorld.entity.StatusCode;
import com.LearnInWorld.video.feign.CategoryFeign;
import com.LearnInWorld.video.feign.ModuleFeign;
import com.LearnInWorld.video.pojo.Category;
import com.LearnInWorld.video.pojo.Module;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/webPage")
public class WebPageController {
    @Autowired
    private ModuleFeign moduleFeign;
    @Autowired
    private CategoryFeign categoryFeign;

    /**
     * 访问首页
     *
     * @return
     */
    @GetMapping("/index")
    public String index(Model model) {
        List<Module> moduleList = moduleFeign.findAll().getData();
        //封装数据
        List<Module> moduleArrayList = envelopedData(moduleList);
        model.addAttribute("moduleList", moduleArrayList);
        return "index";

    }

    /* *//**
     * 访问查询页面
     *
     * @return
     *//*
    @GetMapping("/courseList")
    @ResponseBody
    public Result courseList(@RequestParam("keywords") String keywords, Model model) {

        return new Result(true, StatusCode.OK, "访问成功");
    }*/

    /**
     * 访问查询页面
     *
     * @return
     */
    @GetMapping("/courseList")
    public String courseList(Model model) {
        //渲染模块数据区域
        Module module = new Module();
        module.setModuleName("全部");
        List<Module> moduleList = new ArrayList<>();
        moduleList.add(module);
        List<Module> resultList = moduleFeign.findAll().getData();
        for (Module m : resultList) {
            moduleList.add(m);
        }
        model.addAttribute("moduleList", moduleList);
        return "course_list";

    }

    /**
     * 访问课程
     *
     * @return
     */
    @GetMapping("/courseDetail")
    public String courseDetail() {
        return "course_detail";
    }

    //封装查询数据
    //别问为什么一个渲染这么麻烦,都是前端模板不支持的原因
    private List<Module> envelopedData(List<Module> moduleList) {
        int count = 0;
        for (Module module : moduleList) {
            count++;

            List<Category> categoryList = categoryFeign.findCategoryByModule((int) module.getModuleId());

            int categoryNum = 0; //记录循环数
            if (categoryList.size() > 3) { //分类多于3按三算
                categoryNum = 3;
            } else {
                categoryNum = categoryList.size();
            }
            List<Category> newCategoryList = new ArrayList<>();
            for (int i = 1; i < categoryNum; i++) {
                Category category = categoryList.get(i);
                newCategoryList.add(category);
            }
            module.setCategoryList(newCategoryList);
            if (count == 7) {
                break;
            }
        }
        List<Module> moduleArrayList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            moduleArrayList.add(moduleList.get(i));
        }
        return moduleArrayList;
    }


}
